package com.wgchao.identityrightsmanagement.application.controller;

import com.wgchao.identityrightsmanagement.application.service.AgencyApplicationService;
import com.wgchao.identityrightsmanagement.domain.organization.model.AgencyCreator;
import com.wgchao.identityrightsmanagement.domain.organization.model.AgencyUpdator;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.infrastructure.utils.ResponseData;
import com.wgchao.identityrightsmanagement.query.AgencyQuery;
import com.wgchao.identityrightsmanagement.query.pageparam.AgencyPageParam;
import com.wgchao.identityrightsmanagement.query.vo.AgencyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: AgencyController
 * @author: wgchao
 * @createTime: 2020/6/12 11:24 AM
 * @Description: 机构信息管理
 */
@RestController
@RequestMapping("/sntk/sys/base/agency")
public class AgencyController {
    @Autowired
    private AgencyApplicationService agencyApplicationService;
    @Autowired
    private AgencyQuery agencyQuery;

    /**
     * 添加机构
     * @param agencyCreator
     * @return
     */
    @PostMapping
    public ResponseData addAgency(@RequestBody AgencyCreator agencyCreator){
        agencyApplicationService.addNewAgency(agencyCreator, "admin");
        return ResponseData.success("添加机构成功");
    }

    /**
     * 修改机构信息
     * @param agencyCode
     * @param agencyUpdator
     * @return
     */
    @PutMapping("/{agencyCode}")
    public ResponseData modifyAgencyInfo(@PathVariable String agencyCode,
                                   @RequestBody AgencyUpdator agencyUpdator){
        agencyApplicationService.modifyAgencyInfo(agencyCode, agencyUpdator, "admin");
        return ResponseData.success("修改机构信息成功");
    }

    /**
     * 禁用机构
     * @param agencyCode
     * @return
     */
    @PutMapping("/disabled/{agencyCode}")
    public ResponseData disabled(@PathVariable String agencyCode){
        agencyApplicationService.disable(agencyCode, "admin");
        return ResponseData.success();
    }

    /**
     * 启用机构
     * @param agencyCode
     * @return
     */
    @PutMapping("/regain/{agencyCode}")
    public ResponseData regain(@PathVariable String agencyCode){
        agencyApplicationService.regain(agencyCode, "admin");
        return ResponseData.success();
    }

    /**
     * 删除机构
     * @param agencyCode
     * @return
     */
    @DeleteMapping("/{agencyCode}")
    public ResponseData remove(@PathVariable String agencyCode){
        agencyApplicationService.removeAgency(agencyCode, "admin");
        return ResponseData.success();
    }

    /**
     * 删除机构
     * @param agencyCodes
     * @return
     */
    @DeleteMapping
    public ResponseData removeAll(@RequestBody List<String> agencyCodes){
        agencyApplicationService.removeAgencyList(agencyCodes, "admin");
        return ResponseData.success();
    }

    /**
     * 通过机构编号查询机构详细信息
     * @param agencyCode
     * @return
     */
    @GetMapping("/{agencyCode}")
    public ResponseData<AgencyVo> findByAgencyCode(@PathVariable String agencyCode){
        AgencyVo agencyVo = agencyQuery.findByCode(agencyCode);
        return ResponseData.success().setData(agencyVo);
    }

    /**
     * 条件分页查询
     * @param agencyPageParam
     * @return
     */
    @GetMapping("/page/condition")
    public ResponseData<PageData<AgencyVo>> findByPage(AgencyPageParam agencyPageParam){
        PageData<AgencyVo> agencyVoPageData = agencyQuery.findByPage(agencyPageParam);
        return ResponseData.success().setData(agencyVoPageData);
    }
}
